<script lang="ts" setup>
import type { SystemOperateCategoriesApi } from '#/api/system/operateCategories';

import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { getOperateCategories } from '#/api/system/operateCategories';
import { useDescription } from '#/components/description';

import { useDetailSchema } from '../data';
import ReviewNodeTable from './ReviewNodeTable.vue';

const reviewRate = ref<number>(0);
const reviewNodeTableRef = ref<InstanceType<typeof ReviewNodeTable>>();
const formData = ref<SystemOperateCategoriesApi.OperateCategories>();
const [Descriptions] = useDescription({
  bordered: true,
  size: 'middle',
  column: 2,
  labelWidth: 120,

  schema: useDetailSchema(),
});

const [Modal, modalApi] = useVbenModal({
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    let data = modalApi.getData<SystemOperateCategoriesApi.OperateCategories>();
    if (!data || !data.id) {
      reviewRate.value = data?.metadata?.reviewRate || 0;
      return;
    }
    modalApi.lock();
    try {
      data = await getOperateCategories(data.id);
      reviewRate.value = data.metadata?.reviewRate || 0;
      formData.value = data;
    } finally {
      modalApi.unlock();
    }
  },
});
</script>

<template>
  <Modal
    title="查看 业务分类"
    class="w-[80%]"
    :show-cancel-button="false"
    :show-confirm-button="false"
  >
    <Descriptions :data="formData" class="mx-4" />
    <ReviewNodeTable
      ref="reviewNodeTableRef"
      class="mx-4"
      v-if="reviewRate > 0"
      :disabled="true"
      :value="formData?.metadata?.reviewList"
      :num="reviewRate"
    />
  </Modal>
</template>
